package name.zxw.datastructures.recursion;

/**
 * @author ZhangXinWang
 * @date 2023/5/15 22:32
 */
public class Maze {

    public static void main(String[] args) {

        //初始化地图
        int[][] map = new int[8][7];
        for (int i = 0; i < 7; i++) {
            map[0][i] = 1;
            map[7][i] = 1;
        }
        for (int i = 0; i < 8; i++) {
            map[i][0] = 1;
            map[i][6] = 1;
        }
        map[3][1] = 1;
        map[3][2] = 1;

//        map[6][5] = 3;
        printMap(map);

        setWay(map, 1, 1);


    }

    /**
     * 递归找路-下右上左
     *
     * @param map 地图
     * @param i   初始位置横坐标
     * @param j   初始位置纵坐标
     * @return
     */
    public static boolean setWay(int[][] map, int i, int j) {
        if (map[6][5] == 2) {
            return true;
        } else {
            if (map[i][j] == 0) {
                map[i][j] = 2;
                printMap(map);
                if (setWay(map, i + 1, j)) {
                    return true;
                } else if (setWay(map, i, j + 1)) {
                    return true;
                } else if (setWay(map, i - 1, j)) {
                    return true;
                } else if (setWay(map, i, j - 1)) {
                    return true;
                } else {
                    //表示该节点是死路
                    map[i][j] = 3;
                    printMap(map);
                }
            } else {
                return false;
            }
        }
        return false;
    }

    private static int count;

    public static void printMap(int[][] map) {
        for (int i = 0; i < map.length; i++) {
            int[] row = map[i];
            for (int j = 0; j < row.length; j++) {
                int element = row[j];
                System.out.print(element);
            }
            System.out.println();
        }
        System.out.println("---------------" + ++count + "------------");
    }

}
